-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
SiteInformationWizardContent.swift
423 lines (333 loc) · 14.9 KB
/
SiteInformationWizardContent.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import AutomatticTracks
import UIKit
import WordPressAuthenticator
typealias SiteInformationCompletion = (SiteInformation) -> Void
final class SiteInformationWizardContent: UIViewController {
private enum Row: Int, CaseIterable {
case title = 0
case tagline = 1
func matches(_ row: Int) -> Bool {
return row == self.rawValue
}
}
private struct Constants {
static let bottomMargin: CGFloat = 0.0
static let footerHeight: CGFloat = 42.0
static let footerVerticalMargin: CGFloat = 6.0
static let footerHorizontalMargin: CGFloat = 16.0
static let rowHeight: CGFloat = 44.0
}
private let completion: SiteInformationCompletion
@IBOutlet weak var table: UITableView!
@IBOutlet weak var nextStep: NUXButton!
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
@IBOutlet weak var buttonWrapper: ShadowView!
private lazy var headerData: SiteCreationHeaderData = {
let title = NSLocalizedString("Basic information", comment: "Create site, step 3. Select basic information. Title")
let subtitle = NSLocalizedString("Tell us more about the site you are creating.", comment: "Create site, step 3. Select basic information. Subtitle")
return SiteCreationHeaderData(title: title, subtitle: subtitle)
}()
init(completion: @escaping SiteInformationCompletion) {
self.completion = completion
super.init(nibName: String(describing: type(of: self)), bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
applyTitle()
setupBackground()
setupTable()
setupButtonWrapper()
setupNextButton()
WPAnalytics.track(.enhancedSiteCreationBasicInformationViewed)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
startListeningToKeyboardNotifications()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
postScreenChangedForVoiceOver()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
stopListeningToKeyboardNotifications()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
table.layoutHeaderView()
}
private func applyTitle() {
title = NSLocalizedString("2 of 3", comment: "Site creation. Step 2. Screen title")
}
private func setupBackground() {
view.backgroundColor = .listBackground
}
private func setupTable() {
setupTableBackground()
setupTableSeparator()
registerCell()
setupCellHeight()
setupHeader()
setupFooter()
setupConstraints()
table.dataSource = self
}
private func setupTableBackground() {
table.backgroundColor = .listBackground
}
private func setupTableSeparator() {
table.separatorColor = .divider
}
private func registerCell() {
table.register(
InlineEditableNameValueCell.defaultNib,
forCellReuseIdentifier: InlineEditableNameValueCell.defaultReuseID
)
}
private func setupCellHeight() {
table.rowHeight = UITableView.automaticDimension
table.estimatedRowHeight = Constants.rowHeight
}
private func setupButtonWrapper() {
buttonWrapper.backgroundColor = .listBackground
}
private func setupNextButton() {
nextStep.addTarget(self, action: #selector(goToNextStep), for: .touchUpInside)
setupButtonAsSkip()
}
private func setupButtonAsSkip() {
let buttonTitle = NSLocalizedString("Skip", comment: "Button to progress to the next step")
nextStep.setTitle(buttonTitle, for: .normal)
nextStep.accessibilityLabel = buttonTitle
nextStep.accessibilityHint = NSLocalizedString("Navigates to the next step without making changes", comment: "Site creation. Navigates to the next step")
nextStep.isPrimary = false
}
private func setupButtonAsNext() {
let buttonTitle = NSLocalizedString("Next", comment: "Button to progress to the next step")
nextStep.setTitle(buttonTitle, for: .normal)
nextStep.accessibilityLabel = buttonTitle
nextStep.accessibilityHint = NSLocalizedString("Navigates to the next step saving changes", comment: "Site creation. Navigates to the next step")
nextStep.isPrimary = true
}
private func setupHeader() {
let initialHeaderFrame = CGRect(x: 0, y: 0, width: Int(table.frame.width), height: 0)
let header = TitleSubtitleHeader(frame: initialHeaderFrame)
header.setTitle(headerData.title)
header.setSubtitle(headerData.subtitle)
table.tableHeaderView = header
NSLayoutConstraint.activate([
header.widthAnchor.constraint(equalTo: table.widthAnchor),
header.centerXAnchor.constraint(equalTo: table.centerXAnchor),
])
}
private func setupFooter() {
let footer = UIView(frame: CGRect(x: 0.0, y: 0.0, width: table.frame.width, height: Constants.footerHeight))
let title = UILabel(frame: .zero)
title.translatesAutoresizingMaskIntoConstraints = false
title.textAlignment = .natural
title.numberOfLines = 0
title.textColor = .neutral(.shade50)
title.font = WPStyleGuide.fontForTextStyle(.footnote, fontWeight: .regular)
title.text = TableStrings.footer
title.adjustsFontForContentSizeCategory = true
footer.addSubview(title)
NSLayoutConstraint.activate([
title.leadingAnchor.constraint(equalTo: footer.leadingAnchor, constant: Constants.footerHorizontalMargin),
title.trailingAnchor.constraint(equalTo: footer.trailingAnchor, constant: -1 * Constants.footerHorizontalMargin),
title.topAnchor.constraint(equalTo: footer.topAnchor, constant: Constants.footerVerticalMargin)
])
table.tableFooterView = footer
}
private func setupConstraints() {
table.cellLayoutMarginsFollowReadableWidth = true
NSLayoutConstraint.activate([
table.leadingAnchor.constraint(equalTo: view.prevailingLayoutGuide.leadingAnchor),
table.trailingAnchor.constraint(equalTo: view.prevailingLayoutGuide.trailingAnchor),
])
}
@objc
private func goToNextStep() {
let collectedData = SiteInformation(title: titleString(), tagLine: taglineString())
completion(collectedData)
trackBasicInformationNextStep()
}
private func trackBasicInformationNextStep() {
if nextStep.isPrimary {
let basicInformationProperties: [String: AnyObject] = [
"site_title": titleString() as AnyObject,
"tagline": taglineString() as AnyObject
]
WPAnalytics.track(.enhancedSiteCreationBasicInformationCompleted, withProperties: basicInformationProperties)
} else {
WPAnalytics.track(.enhancedSiteCreationBasicInformationSkipped)
}
}
// MARK: - Cell Titles
private func titleString() -> String {
return valueText(for: Row.title)
}
private func taglineString() -> String {
return valueText(for: Row.tagline)
}
// MARK: - Accessing Cells
private func indexPath(for row: Row) -> IndexPath {
return IndexPath(row: row.rawValue, section: 0)
}
private func cell(for row: Row) -> InlineEditableNameValueCell? {
return cell(at: indexPath(for: row))
}
private func cell(at indexPath: IndexPath) -> InlineEditableNameValueCell? {
return table.cellForRow(at: indexPath) as? InlineEditableNameValueCell
}
// MARK: - Cell Value Text Fields
private func valueTextField(at indexPath: IndexPath) -> UITextField? {
return cell(at: indexPath)?.valueTextField
}
private func valueTextField(for row: Row) -> UITextField? {
return cell(for: row)?.valueTextField
}
private func valueText(for row: Row) -> String {
return valueTextField(for: row)?.text ?? ""
}
}
extension SiteInformationWizardContent: UITableViewDataSource {
private enum TableStrings {
static let site = NSLocalizedString("Site Title", comment: "Site info. Title")
static let tagline = NSLocalizedString("Tagline", comment: "Site info. Tagline")
static let footer = NSLocalizedString("The tagline is a short line of text shown right below the title.", comment: "Site info. Table footer.")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Row.allCases.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: InlineEditableNameValueCell.defaultReuseID, for: indexPath) as? InlineEditableNameValueCell else {
assertionFailure("SiteInformationWizardContent. Could not dequeue a cell")
return UITableViewCell()
}
configure(cell, index: indexPath)
return cell
}
private func configure(_ cell: InlineEditableNameValueCell, index: IndexPath) {
if Row.title.matches(index.row) {
cell.nameLabel.text = TableStrings.site
cell.valueTextField.attributedPlaceholder = attributedPlaceholder(text: TableStrings.site)
cell.valueTextField.delegate = self
cell.valueTextField.returnKeyType = .next
cell.addTopBorder(withColor: .neutral(.shade10))
}
if Row.tagline.matches(index.row) {
cell.nameLabel.text = TableStrings.tagline
cell.valueTextField.attributedPlaceholder = attributedPlaceholder(text: TableStrings.tagline)
cell.valueTextField.delegate = self
cell.valueTextField.returnKeyType = .done
cell.addBottomBorder(withColor: .neutral(.shade10))
}
cell.contentView.backgroundColor = .listForeground
cell.nameLabel.font = WPStyleGuide.fontForTextStyle(.body, fontWeight: .regular)
cell.nameLabel.textColor = .text
cell.nameLabel.backgroundColor = .listForeground
cell.valueTextField.font = WPStyleGuide.fontForTextStyle(.body, fontWeight: .regular)
cell.valueTextField.textColor = .text
cell.valueTextField.backgroundColor = .listForeground
if cell.delegate == nil {
cell.delegate = self
}
}
private func attributedPlaceholder(text: String) -> NSAttributedString {
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.textPlaceholder,
.font: WPStyleGuide.fontForTextStyle(.body, fontWeight: .regular)
]
return NSAttributedString(string: text, attributes: attributes)
}
}
extension SiteInformationWizardContent: InlineEditableNameValueCellDelegate {
func inlineEditableNameValueCell(_ cell: InlineEditableNameValueCell,
valueTextFieldDidChange text: String) {
updateButton()
}
private func updateButton() {
formIsFilled() ? setupButtonAsNext() : setupButtonAsSkip()
}
private func formIsFilled() -> Bool {
return !titleString().isEmpty || !taglineString().isEmpty
}
}
extension SiteInformationWizardContent: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
for (index, row) in Row.allCases.enumerated() {
guard let rowTextField = valueTextField(for: row) else {
let errorMessage = "We expect all rows to have `valueTextField` but row \(index) doesn't. Please review the logic."
CrashLogging.logMessage(errorMessage, properties: nil, level: .error)
assertionFailure(errorMessage)
continue
}
guard rowTextField == textField else {
continue
}
let indexPath = IndexPath(row: index + 1, section: 0)
guard let nextTextField = valueTextField(at: indexPath) else {
goToNextStep()
return false
}
nextTextField.becomeFirstResponder()
return false
}
return true
}
}
extension SiteInformationWizardContent {
private func startListeningToKeyboardNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
private func stopListeningToKeyboardNotifications() {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc
private func keyboardWillShow(_ notification: Foundation.Notification) {
guard let payload = KeyboardInfo(notification) else { return }
let keyboardScreenFrame = payload.frameEnd
let convertedKeyboardFrame = view.convert(keyboardScreenFrame, from: nil)
var constraintConstant = convertedKeyboardFrame.height
let bottomInset = view.safeAreaInsets.bottom
constraintConstant -= bottomInset
let animationDuration = payload.animationDuration
bottomConstraint.constant = constraintConstant
view.setNeedsUpdateConstraints()
if table.frame.contains(convertedKeyboardFrame.origin) {
let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: constraintConstant, right: 0.0)
table.contentInset = contentInsets
table.scrollIndicatorInsets = contentInsets
buttonWrapper.addShadow()
}
UIView.animate(withDuration: animationDuration,
delay: 0,
options: .beginFromCurrentState,
animations: { [weak self] in
self?.view.layoutIfNeeded()
},
completion: nil)
}
@objc
private func keyboardWillHide(_ notification: Foundation.Notification) {
buttonWrapper.clearShadow()
bottomConstraint.constant = Constants.bottomMargin
}
}
// MARK: - VoiceOver
private extension SiteInformationWizardContent {
func postScreenChangedForVoiceOver() {
UIAccessibility.post(notification: .screenChanged, argument: table.tableHeaderView)
}
}